package com.lzm.ydpt.view.pop;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.byt.framlib.R;
import com.byt.framlib.baseadapter.lv.LvCommonAdapter;
import com.byt.framlib.baseadapter.lv.LvViewHolder;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.lzm.ydpt.entity.mall.CouponBean;

import java.util.List;


/**
 * 使用优惠券的Pop
 */
public class CouponPopWindows extends PopupWindow {
    private Context mContext;
    private ImageView img_dismiss_currency_pop;
    private ListView lv_show_coupon_data;
    private ListViewOnImtermClick mListViewOnImtermClick;
    private List<CouponBean> mCouponBean;

    /**
     * @param context
     * @param width
     * @param height
     */
    @SuppressLint("NewApi")
    public CouponPopWindows(Context context, int width, int height, List<CouponBean> couponBeans, ListViewOnImtermClick listViewOnImtermClick) {
        super();
        this.mContext = context;
        this.mListViewOnImtermClick = listViewOnImtermClick;
        this.mCouponBean = couponBeans;
        setFocusable(true);
        // 设置弹窗内可点击
        setTouchable(true);
        // 设置弹窗外可点击
        setOutsideTouchable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        // this.setAnimationStyle(R.style.mystyle);
        // 加上这个只是为了让返回键点击能消失popWindow
        setBackgroundDrawable(new BitmapDrawable());
        // 设置弹窗的宽度和高度
        setWidth(width);
        setHeight(height);
        // 设置弹窗的布局界面
        setContentView(LayoutInflater.from(mContext).inflate(R.layout.popwindow_coupon_data, null));
        initUi();
    }

    private void initUi() {
        img_dismiss_currency_pop = getContentView().findViewById(R.id.img_dismiss_currency_pop);
        lv_show_coupon_data = getContentView().findViewById(R.id.lv_show_coupon_data);
        lv_show_coupon_data.setAdapter(new LvCommonAdapter<CouponBean>(mContext, mCouponBean, R.layout.item_pop_coupon_lv) {
            @Override
            protected void convert(LvViewHolder viewHolder, CouponBean item, int position) {
                viewHolder.setText(R.id.tv_v_currency_amount, item.getMoney());
                viewHolder.setText(R.id.tv_show_all_data, "满" + item.getAll() + "使用");
                viewHolder.setText(R.id.tv_use_coupon_time, "有效期" + item.getTime());
                viewHolder.setOnClickListener(R.id.tv_use_coupon_now, new OnNoDoubleClickListener() {
                    @Override
                    protected void onNoDoubleClick(View v) {
                        if (mListViewOnImtermClick != null) {
                            mListViewOnImtermClick.onLvOnItem(position);
                        }
                    }
                });
            }
        });
        img_dismiss_currency_pop.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                dismiss();
            }
        });

    }

    public void showPop(View v) {
        showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
    }

    public interface ListViewOnImtermClick {
        void onLvOnItem(int position);
    }
}